package dao.impl;

import Utils.db.dao.BaseDAO;
import dao.UserDAO;
import pojo.User;

import java.sql.SQLException;
import java.util.List;

/**
 * @author d11yu
 */
public class UserDAOImpl extends BaseDAO implements UserDAO {
    @Override
    public User addUser(User user) throws SQLException {
        String sql = "insert into user (username, password, type) values (?, ?, ?)";
        update(sql, user.getUsername(), user.getPassword(), user.getType());
        return queryUserByUsername(user.getUsername());
    }

    @Override
    public Integer deleteUserById(Integer id) {
        String sql = "delete from user where id = ?";
        return update(sql, id);
    }

    @Override
    public Integer deleteUserByUserame(String username) {
        String sql = "delete from user where username = ?";
        return update(sql, username);
    }

    @Override
    public User queryUserByUsernameAndPassword(String username, String password) throws SQLException {
        String sql = "select id, username, password, type from user where username = ? and password = ?";
        return queryBean(User.class, sql, username, password);
    }

    @Override
    public User queryUserByUsername(String username) throws SQLException {
        String sql = "select id, username, password, type from user where username = ?";
        return queryBean(User.class, sql, username);
    }

    @Override
    public Integer updateUserById(User user) {
        String sql = "update user set username = ?, password = ?, type = ? where id = ?";
        return update(sql, user.getUsername(), user.getPassword(), user.getType(), user.getId());
    }

    @Override
    public List<User> queryUsersByUsernameBlur(String username) throws SQLException {
        String sql = "select id, username, password, type from user where username like ?";
        return queryBeanList(User.class, sql, "%" + username + "%");
    }

    @Override
    public Integer getEct() throws SQLException {
        String sql = "select count(*) from user";
        return ((Number) queryScalar(sql)).intValue();
    }

    /**
     * @return
     * @throws SQLException
     */
    @Override
    public Integer getEctByUsernameBlur(String username) throws SQLException {
        String sql = "select count(*) from user where username like ?";
        return ((Number) queryScalar(sql, "%" + username + "%")).intValue();
    }

    @Override
    public List<User> queryBeanListPerPage(Integer eStart, Integer ecp) throws SQLException {
        String sql = "select id, username, password, type from user limit ?, ?";
        return queryBeanList(User.class, sql, eStart, ecp);
    }

    /**
     * @param eStart
     * @param ecp
     * @return
     * @throws SQLException
     */
    @Override
    public List<User> queryBeanListPerPageByUsernameBlur(Integer eStart, Integer ecp, String username) throws SQLException {
        String sql = "select id, username, password, type from user where username like ? limit ?, ?";
        return queryBeanList(User.class, sql, "%" + username + "%", eStart, ecp);
    }

    @Override
    public User queryById(Integer id) throws SQLException {
        String sql = "select id, username, password, type from user where id = ?";
        return queryBean(User.class, sql, id);
    }
}
